package com.clown.smsbase.Domain;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.hibernate.cfg.AccessType;
import org.springframework.data.jpa.repository.Temporal;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

@MappedSuperclass
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })
@Access (value = AccessType.FIELD)
public class BaseEntity<PK extends Serializable> extends BaseDomain {

    /**
     *
     */
    private static final long serialVersionUID = -6163675075289529459L;
    /**
     * 实体创建时间
     */
    @Temporal (TemporalType.TIMESTAMP)
    @Column (name = "DATECREATED")
    protected Date dateCreated = new Date();

    /**
     * 实体修改时间
     */
    @Temporal( TemporalType.TIMESTAMP)
    @Column(name = "DATEMODIFIED")
    protected Date dateModified = new Date();



    @Id
    @Column(name = "ID")
    @GeneratedValue (strategy = GenerationType.AUTO)
    protected PK id;


    @Override
    public boolean equals(Object obj) {
        if (null != obj) {
            if (obj instanceof com.clown.smsbase.Domain.BaseEntity) {
                @SuppressWarnings("unchecked")
                com.clown.smsbase.Domain.BaseEntity<PK> domain =  (com.clown.smsbase.Domain.BaseEntity<PK>) obj;
                if (this.id == domain.id) {
                    return true;
                }
            }
        }
        return false;
    }

    public Date getDateCreated() {
        return dateCreated;
    }

    public Date getDateModified() {
        return dateModified;
    }



    public PK getId() {
        return id;
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this.id);
    }

    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }

    public void setDateModified(Date dateModified) {
        this.dateModified = dateModified;
    }

    public void setId(PK id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
    }

}
